
<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"
    import ="java.io.*"
	import ="java.net.*"
	import ="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%


	
	request.setCharacterEncoding("UTF-8");
	String view = request.getParameter("query");
	String dview=URLEncoder.encode(view,"UTF-8");
	
	
	String key="b678f273d9f4fd8232e4dac0bd8c8288";
	String line;
	String data=null;
	try {
		InputStream is =new URL("http://openapi.naver.com/search?key="+key+
				"&query="+dview+"&target=image&start=1&display=10").openStream();
		BufferedReader br=new BufferedReader(
				new InputStreamReader(is,"UTF-8")); 
		StringBuilder sb=new StringBuilder();
		while((line=br.readLine())!=null){				
			sb.append(line+"\n");
		}
		data=sb.toString();
	}catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}	
	int a=0,c=0;
	boolean b=false;
	int s=0;
	%><table border="1"><%
	for(int i=0;i<data.length();i++)
	{		
		if(data.charAt(i)=='<'){
			
			if(data.substring(i, i+7).equals("<title>"))
			{							
				s=i+7;
				b=true;
			}else if(b && data.substring(i, i+7).equals("</title"))
			{
				if(a!=0){
					String res=data.substring(s, i);	
					%><tr><td><%=res%></td><%
					b=false;
				}else a=1;
			}
			if(data.substring(i, i+6).equals("<link>"))
			{
				s=i+6;
				b=true;
			}else if(b && data.substring(i, i+7).equals("</link>"))
			{
				if(c!=0){
					String res=data.substring(s, i);
					%><td><img src="
						<%=res%>" width="300"></td></tr><%
					b=false;
				}else c=1;
			}					
		}
	}
	%></table>

</body>
</html>